package gui;

import java.util.Vector;
import java.io.*;

public class SearchSeq {
	private static Vector<Object> mirnames;
	private static Vector<Object> mirvalues;
	private static Vector<Object> targetnames;
	private static Vector<Object> targetvalues;
	private static Vector<Object> hit_str;
	
	public SearchSeq() {
		mirnames = new Vector<Object>();
		mirvalues = new Vector<Object>();
		targetnames = new Vector<Object>();
		targetvalues = new Vector<Object>();
		hit_str = new Vector<Object>();
	}
	public int find_num_hits(String qu, String ref, boolean verbose) {
		int result = 0;
		//string subs;
		if(verbose) {
			System.out.print("Found string ");
			System.out.print(qu);
			System.out.println(" at positions: ");
		}
		for(int it=0; it!=ref.length(); ++it) {
			if(it+qu.length() > ref.length())
				break;
			int res = ref.substring(it, it+qu.length()).compareTo(qu);
			if(res==0) {
				if(verbose) {
					System.out.print(it);
					System.out.print(", ");
				}
				result += 1;
			}
		}
		if(verbose)
			System.out.println("");
		return(result);
	}
	
	int read_fasta(String filename, boolean readtargets)
	{
		try {
			// Open the file that is the first 
			// command line parameter
			FileInputStream fstream = new FileInputStream(filename);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
	        BufferedReader br = new BufferedReader(new InputStreamReader(in));
	        String strLine;
	        //Read File Line By Line
	        while ((strLine = br.readLine()) != null)   {
	        	if(readtargets==true) {
	        		if(strLine.substring(0,1).compareTo(">")==0) {
	        			targetnames.add(strLine);
	        		} else {
	        			targetvalues.add(strLine);
	        		}
	        	} else {
	        		if(strLine.substring(0,1).compareTo(">")==0) {
	        			mirnames.add(strLine);
	        		} else {
	        			mirvalues.add(strLine);
	        		}
	        	}
	        }
	        //Close the input stream
	        in.close();
	    } catch (Exception e){//Catch exception if any
	    	System.err.println("Error: " + e.getMessage());
	    }
	    int ret;
	    if(readtargets)
	    	ret = targetnames.size();
	    else
	    	ret = mirnames.size();
	    return(ret);
	}

	public void find_hits() {
		// make the hitmatrix:
		int nmiRs = mirvalues.size();
		int ntargets = targetvalues.size();
		//Vector<String> hit_str;
		// count the number of occurrences of each seed sequence in each target and record in matrix
		String mir, target;
		Integer num;
		hit_str.add("\" \"\t\"pcontr\"\t\"ncontr\"\t\"4xmiR-155 sponge\"");
		String str = "";
		for(int i=0; i!=nmiRs; ++i) {
		  mir = mirvalues.get(i).toString();
		  str = str.concat(" \"");
		  str = str.concat(mirnames.get(i).toString());
		  str = str.concat("\"\t");
		  for(int j=0; j!=ntargets; ++j) {
		    target = targetvalues.get(j).toString();	
		    // find the number of hits
		    num = find_num_hits(mir,target,false);
		    str = str.concat(num.toString());
		    str = str.concat("\t");
		  }
		  str = str.concat("\n");
		}
		//System.out.println(str);
		hit_str.add(str);
		return; //(hit_str);
	}
	public String convert_to_String() {
		String str = "";
		for(int i=0; i!=hit_str.size(); ++i) {
			//System.out.println(hit_str.get(i));
			str = str.concat(hit_str.get(i).toString());
			str = str.concat("\n");
		}
		return str;
	}
	public void writeFile(String filename) {
		try{
			// Create file 
			FileWriter fstream = new FileWriter(filename);
			BufferedWriter out = new BufferedWriter(fstream);
			for(int i=0; i!=hit_str.size(); ++i) {
				out.write(hit_str.get(i).toString());
			}
			//Close the output stream
			out.close();
		} catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}
}
